package com.huangliusong.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huangliusong.dataobject.OrderMaster;
import com.huangliusong.enums.OrderStatusEnum;
import com.huangliusong.enums.PayStatusEnum;
import com.huangliusong.enums.ResultEnum;
import com.huangliusong.repository.OrderMasterRepository;
import com.huangliusong.service.OrderService;
import com.huangliusong.utils.KeyUtils;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMasterRepository repositiry;

	@Override
	@Transactional
	public OrderMaster create(OrderMaster orderMaster) {
		String orderId = KeyUtils.genUniqueKey();
		orderMaster.setOrderId(orderId);
		return repositiry.save(orderMaster);
	}

	@Override
	@Transactional
	public OrderMaster findOne(String orderId) {
		OrderMaster orderMaster = repositiry.findById(orderId).get();
		if (orderMaster == null) {
			throw new EasySendException(ResultEnum.ORDER_NOT_EXIST);
		}
		return orderMaster;
	}

	@Override
	public Page<OrderMaster> findList(String openId, Pageable pageable) {
		Page<OrderMaster> orderMasterPage = repositiry.findByOpenId(openId, pageable);
		return orderMasterPage;
	}

	@Override
	@Transactional
	public OrderMaster cancel(OrderMaster orderMaster) {
		// 判断订单状态 如果已经接单则不可取消
		if (!orderMaster.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
			System.err.println("取消订单,订单状态不正确" + orderMaster.getOrderStatus());
			throw new EasySendException(ResultEnum.ORDER_STATUS_ERROR);
		}
		// 修改订单状态
		orderMaster.setOrderStatus(OrderStatusEnum.CANCEL.getCode());
		OrderMaster updateResult = repositiry.save(orderMaster);
		if (updateResult == null) {
			System.err.println("订单更新失败");
			throw new EasySendException(ResultEnum.ORDER_UPDATE_FAIL);
		}
		// 如果已支付则退款
		if (orderMaster.getPayStatus().equals(PayStatusEnum.SUCCESS.getCode())) {
			// TODO
		}
		return orderMaster;
	}

	@Override
	@Transactional
	public OrderMaster finish(OrderMaster orderMaster) {
		// 判断订单状态
		if (!orderMaster.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
			System.err.println("完结订单 ， 订单状态不正确:" + orderMaster.getOrderStatus() + " " + orderMaster.getOrderId());
			throw new EasySendException(ResultEnum.ORDER_STATUS_ERROR);
		}
		orderMaster.setOrderStatus(OrderStatusEnum.FINISHED.getCode());
		// 修改状态
		OrderMaster updateResult = repositiry.save(orderMaster);
		if (updateResult == null) {
			System.err.println("订单更新失败");
			throw new EasySendException(ResultEnum.ORDER_UPDATE_FAIL);
		}
		return orderMaster;
	}

	@Override
	@Transactional
	public OrderMaster paid(OrderMaster orderMaster) {
		// 查询订单状态
		if (!orderMaster.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
			System.err.println("订单支付完成，订单状态不正确：" + orderMaster.getOrderId() + " " + orderMaster.getOrderStatus());
			throw new EasySendException(ResultEnum.ORDER_UPDATE_FAIL);
		}
		// 查询支付状态
		if (!orderMaster.getPayStatus().equals(PayStatusEnum.WAIT.getCode())) {
			System.err.println("订单支付完成，订单支付状态不正确" + orderMaster.getOrderId() + " " + orderMaster.getOrderStatus());
			throw new EasySendException(ResultEnum.ORDER_PAY_STATUS_ERROR);
		}

		orderMaster.setPayStatus(PayStatusEnum.SUCCESS.getCode());

		// 修改支付状态
		OrderMaster updateResult = repositiry.save(orderMaster);
		if (updateResult == null) {
			System.err.println("订单支付完成,更新失败" + orderMaster);
			throw new EasySendException(ResultEnum.ORDER_UPDATE_FAIL);
		}
		return orderMaster;
	}

}
